package com.coder.algorithm.offer_2;

import java.util.Arrays;
import java.util.Locale;

/**
 * 把数组排成最小的数
 *
 * @author yuhushuan
 * @date 2020/6/26 23:47
 */
public class _45_1_SortArrayForMinNumber {

    private static void printMinNumber(Integer[] numbers) {
        if (numbers == null) {
            System.out.println("null");
            return;
        }
        if (numbers.length == 1) {
            System.out.println(numbers[0]);
            return;
        }

        Arrays.sort(numbers, (o1, o2) -> {
            String s1 = o1 + Integer.toString(o2);
            String s2 = o2 + Integer.toString(o1);
            return String.CASE_INSENSITIVE_ORDER.compare(s1, s2);
        });


        System.out.println(Arrays.toString(numbers));
    }

    public static void main(String[] args) {
        // 功能测试
        // 数组中有多个数字
        print(new Integer[]{3, 32, 321});
        print(new Integer[]{34, 2, 21, 45});
        // 数组中有重复的数位
        print(new Integer[]{34, 2, 21, 45, 34, 322, 21});
        // 只有一个数字
        print(new Integer[]{34});

        // 特殊输入测试 null
        print(null);
    }

    private static void print(Integer[] numbers) {
        System.out.println(String.format(Locale.getDefault(),
                "数组：%s\n组成的最小数为：", Arrays.toString(numbers)));
        printMinNumber(numbers);
        System.out.println();
    }

}
